import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class FlutterWebView extends StatefulWidget {
  String? uri;
  String? title;
  FlutterWebView({Key? key, @required this.title, @required this.uri}) : super(key: key);

  @override
  State<FlutterWebView> createState() => _FlutterWebViewState();
}

class _FlutterWebViewState extends State<FlutterWebView> {

  late WebViewController _webViewController;
  
  @override
  void initState() {
    super.initState();
    _webViewController = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(Colors.white)
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          print("加载url的进度:$progress");
        },
        onPageStarted: (String url) {},
        onPageFinished: (String url) {},
        onWebResourceError: (WebResourceError error) {},
        onNavigationRequest: (NavigationRequest request) {
          if (request.url.startsWith('https://www.youtube.com/')) {
            return NavigationDecision.prevent;
          }
          return NavigationDecision.navigate;
        }
      )
    )..loadRequest(Uri.parse(widget.uri ?? ""));
  }
  
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title ?? "flutter_wanandroid"),
        backgroundColor: Theme.of(context).primaryColor,
      ),
      body: WebViewWidget(controller: _webViewController,),
    );
  }
}
